//
//  ThreeCollectionViewController.m
//  12.30_homework_豆瓣电影MVC
//
//  Created by dllo on 16/1/7.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "ThreeCollectionViewController.h"
#import "ThreeModel.h"
#import "ThreeCollectionViewCell.h"
#import "MovieDetailViewController.h"

@interface ThreeCollectionViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) NSMutableArray *arrData;

@end

@implementation ThreeCollectionViewController

- (void)dealloc {
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"电影";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"btn_nav_collection@2x.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:(UIBarButtonItemStylePlain) target:self action:@selector(rightAction:)];
    
    [self dataHandle];
    [self createCollectionView];
}
- (void)rightAction:(UIBarButtonItem *)button {
    
    [self.navigationController popToRootViewControllerAnimated:NO];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _arrData.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    ThreeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    ThreeModel *model = _arrData[indexPath.row];
    
    cell.threeModel = model;
    
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
       
    MovieDetailViewController *movieDetail = [[MovieDetailViewController alloc] init];
    
    movieDetail.indexPath = indexPath;
    
    [self.navigationController pushViewController:movieDetail animated:YES];
}



- (void)createCollectionView {
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumLineSpacing = 10;
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 40) / 3, 200);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 50) collectionViewLayout:layout];
    [self.view addSubview:collection];
    [collection release];
    collection.backgroundColor = [UIColor whiteColor];
    collection.delegate = self;
    collection.dataSource = self;
    
    [collection registerClass:[ThreeCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
}

- (void)dataHandle {

    NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"plist"];
    NSArray *arr = [NSArray arrayWithContentsOfFile:path];
    
    self.arrData = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        ThreeModel *movieModel = [ThreeModel threeModel];
        [movieModel setValuesForKeysWithDictionary:dic];
        [_arrData addObject:movieModel];
    }

//    NSLog(@"%@", _arrData);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
